//
//  ChooseLocationViewController.m
//  LawyerAssistant
//
//  Created by 戴伟 on 2018/11/8.
//  Copyright © 2018年 戴伟. All rights reserved.
//

#import "ChooseLocationViewController.h"
//#import <MAMapKit/MAMapKit.h>
#import <AMapFoundationKit/AMapFoundationKit.h>
#import <AMapSearchKit/AMapSearchKit.h>
#import <AMapLocationKit/AMapLocationKit.h>
#import "LocationCell.h"
#import "UIImage+MMSJ.h"

#import <CoreLocation/CoreLocation.h>

@interface ChooseLocationViewController ()<AMapSearchDelegate, UITextFieldDelegate, UITableViewDataSource, UITableViewDelegate>


@property (nonatomic, strong) AMapSearchAPI *search;

@property (nonatomic, strong) NSMutableArray *dataArr;
@property (weak, nonatomic) IBOutlet UIView *navView;
@property (weak, nonatomic) IBOutlet UITextField *textField;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (weak, nonatomic) IBOutlet UILabel *cityLabel;

@end

@implementation ChooseLocationViewController

-(void)viewDidAppear:(BOOL)animated{
    [super viewDidAppear:animated];
    dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(0.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
        [self.textField becomeFirstResponder];
    });
}

-(AMapSearchAPI *)search{
    if (!_search) {
        _search = [[AMapSearchAPI alloc] init];
        _search.delegate = self;
    }
    return _search;
}

-(NSMutableArray *)dataArr{
    if (!_dataArr) {
        _dataArr = [NSMutableArray array];
    }
    return _dataArr;
}

- (BOOL)fd_prefersNavigationBarHidden{
    return YES;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.title = @"选择位置";
    
    [self.navView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.height.mas_equalTo(SafeAreaTopHeight);
    }];
    _tableView.tableFooterView = [UIView new];
    _cityLabel.text = [User sharedUser].currentCity;
    [[NSNotificationCenter defaultCenter]addObserver:self selector:@selector(textFieldTextDidChange:)
                                                name:UITextFieldTextDidChangeNotification
                                              object:_textField];
    
}




- (void)onPOISearchDone:(AMapPOISearchBaseRequest *)request response:(AMapPOISearchResponse *)response
{
    [self.dataArr removeAllObjects];
    [self.dataArr addObjectsFromArray:response.pois];
    [self.tableView reloadData];

}


-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 74;
}

-(NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{

    return _dataArr.count;
}

-(UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    AMapPOI *location = _dataArr[indexPath.row];
    LocationCell *cell = [LocationCell cellWithTableView:tableView];
    cell.location = location;
    
    return cell;
}

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [self.textField endEditing:YES];
    AMapPOI *location = _dataArr[indexPath.row];
    if (self.chooseCompleteBlock) {
        self.chooseCompleteBlock(location);
    }
    [self.navigationController popViewControllerAnimated:YES];
}

- (IBAction)back:(id)sender {
    [self.navigationController popViewControllerAnimated:YES];
}

-(void)textFieldTextDidChange:(NSNotification *)notification{
    if (_textField.text.length != 0) {
        AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
            
        request.keywords            = _textField.text;
        request.city                = [User sharedUser].currentCity;
        request.types               = @"住宅区|楼宇";
        request.requireExtension    = YES;
            
        request.cityLimit           = YES;
        request.requireSubPOIs      = YES;
        [self.search AMapPOIKeywordsSearch:request];
    }
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    [self.textField endEditing:YES];
    
    AMapPOIKeywordsSearchRequest *request = [[AMapPOIKeywordsSearchRequest alloc] init];
        
    request.keywords            = textField.text;
    request.city                = [User sharedUser].currentCity;
    request.types               = @"住宅区|楼宇";
    request.requireExtension    = YES;
        
    request.cityLimit           = YES;
    request.requireSubPOIs      = YES;
    [self.search AMapPOIKeywordsSearch:request];
    return YES;
}

-(void)scrollViewDidScroll:(UIScrollView *)scrollView{
    [_textField endEditing:YES];
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}
@end
